package org.ccay.manage.security.service.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.core.base.PageVO;
import org.ccay.core.base.PagedResult;
import org.ccay.manage.security.dao.IUserDao;
import org.ccay.manage.security.service.IUserManageService;
import org.ccay.security.authc.UserEncryptUtil;
import org.ccay.security.dao.IUserDimensionDataProviderDao;
import org.ccay.security.vo.UserVO;

@Named
public class UserManageService implements IUserManageService{
	
	@Inject
	private IUserDao userDao; 
	
	@Inject
	private IUserDimensionDataProviderDao userDimensionDataProviderDao; 

	@Override
	public Map<String, String> validate4SaveOrUpdate(UserVO user) {
		Map<String,String> errorMsg = new HashMap<String,String>();
		if(!checkUsernameUnique(user.getName(),user.getId())){
			errorMsg.put("userNameDuplicate", "用户名重复");
		}
		if(!checkUserDisplayNameUnique(user.getDisplayName(),user.getId())){
			errorMsg.put("userDisplayNameDuplicate", "用户显示名称重复");
		}
		return errorMsg;
	}

	
	@Override
	public Map<String, String> createUser(UserVO user) {
		Map<String,String> errorMsg = validate4SaveOrUpdate(user);
		if(!errorMsg.isEmpty()){
			return errorMsg;
		}
		user.setDeleted(false);
		user.setExpireDate(new Date(user.getExpireDate().getTime() + (24 * 60 * 60 * 1000) - 1));
		encryptUserVO(user);
		userDao.addUser(user);
		return Collections.emptyMap();
	}
	

	@Override
	public Map<String, String> updateUser(UserVO user) {
		Map<String,String> errorMsg = validate4SaveOrUpdate(user);
		if(!errorMsg.isEmpty()){
			return errorMsg;
		}
		user.setExpireDate(new Date(user.getExpireDate().getTime() + (24 * 60 * 60 * 1000) - 1));
		user.setDeleted(false);
		encryptUserVO(user);
		userDao.updateUser(user);
		return Collections.emptyMap();
	}

	private void encryptUserVO(UserVO user){
		String [] encrypt = UserEncryptUtil.encrypt(user.getPassword());
		user.setPassword(encrypt[0]);
		user.setSalt(encrypt[1]);
	}
	
	@Override
	public int deleteUser(Long id) {
		return userDao.delUser(id);
	}
	
	@Override
	public int revertUser(Long id) {
		return userDao.revertUser(id);
	}

	@Override
	public boolean checkUsernameUnique(String username, Long userId) {
		return userDao.checkUsernameUnique(username, userId) == 0;
	}

	@Override
	public boolean checkUserDisplayNameUnique(String username, Long userId) {
		return userDao.checkUserDisplayNameUnique(username, userId) == 0;
	}

	@Override
	public PagedResult<UserVO> findPagedUsers(UserVO user, PageVO page) {
		return userDimensionDataProviderDao.findPagedUsers(user, page);
	}

	@Override
	public UserVO findUserById(Long userId) {
		return userDao.findUserById(userId);
	}

	@Override
	public UserVO findUserByName(String userName) {
		return userDao.findUserByName(userName);
	}

	@Override
	public PagedResult<UserVO> findPagedEnabledUsers(UserVO user, PageVO page) {
		user.setEnabled(true);
		return userDimensionDataProviderDao.findPagedUsers(user, page);
	}
	
	
}
